/* eslint-disable */
import {fixZeroInfo} from '../../lib/zeroReportUtils.js'

function openSecondWin(CygnBZ,fqxDm, fqxMc, qxDm, qxMc) {
  window.open("sword?ctrl=WBLoginGdgjCtrl_openSecondWin&CygnBZ="+CygnBZ+"&qxDm=" + qxDm + "&fqxDm=" + fqxDm + "&fqxMc=" + encodeURI(encodeURI(fqxMc)) + "&qxMc=" + encodeURI(encodeURI(qxMc)));
}
const openFakeWin=(url, wbxt_dm, tName, qxDm)=>{
    if("F251000200000136" == wbxt_dm){
        var DivModal = "<div id='dialog-message' style='width: 1000px;'>" +
            "<center><p style='font-size:20px;font-weight: bolder'>温馨提示</p></center>" +
            "<p style='font-size:16px;'>尊敬的纳税人：</p>" +
            "<p style='font-size:16px;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;实名办税，是指税务机关在纳税人办理涉税事项前，对相关人员的实名信息，进行采集和验证的制度。按照国家税务总局积极推进实名办税的相关要求，重庆市电子税务局于2019年5月27日起关闭车辆购置税快捷申报功能，请您通过电子税务局进行注册及实名认证后办理车辆购置税网上申报业务。</p>" +
            "</div>";
        $(document.body).append(DivModal);
        $("#dialog-message").dialog({
            modal: true,
            width: 920,
            height: 600,
            buttons: {
                '关闭': function () {
                    $("#dialog-message").dialog("close");
                }
            }
        });
        return ;
    }
    if(wbxt_dm == "F251000200000093"){
        var h1 = screen.availHeight - 40;
        var w1 = screen.availWidth;
        var win = window.open("https://its.chongqing.chinatax.gov.cn/sjzzy.html", "", "height=" + h1 + ",width=" + w1 + ",top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no", true);
        return;
    }
    if(wbxt_dm == 'F251000200000053'){
        onup1();
        return;
    }
    if(wbxt_dm == 'F251000100000355'){
        loginForGjj();
        return;
    }
    if(wbxt_dm == 'F251000200000257' || wbxt_dm == 'F251000200000258' || wbxt_dm == 'F251000200000259'
        ||wbxt_dm == 'F251000200000260' || wbxt_dm == 'F251000200000261' || wbxt_dm == 'F251000200000262'
        ||wbxt_dm == 'F251000200000263' || wbxt_dm == 'F251000200000264' || wbxt_dm == 'F251000200000265'
        ||wbxt_dm == 'F251000200000266' || wbxt_dm == 'F251000200000267' || wbxt_dm == 'F251000200000268'
        ||wbxt_dm == 'F251000200000269' || wbxt_dm == 'F251000200000245' || wbxt_dm == 'F251000200000246'
        ||wbxt_dm == 'F251000200000247' || wbxt_dm == 'F251000200000248' || wbxt_dm == 'F251000200000249'){
        onup3(url);
        return;
    }
    var keyValue = "";
    $.ajax({
        type: 'post',
        data: {"url": url, "wbxtDm": wbxt_dm , "qxDm" : qxDm},
        url: '/download.sword?ctrl=LogindesCtrl_queryKeyValue',
        dataType: "json",
        success: function (resData) {
            if (resData != null) {
                var xybz = resData.xybz;
                if(xybz == "N"){
                    alert(resData.msg);
                    return ;
                }
                //风险纳税人提示
                var isfxnsr = resData.isfxnsr;
                if(isfxnsr == "Y"){
                    alert("您已被认定为风险纳税人，请及时到主管税务机关处理。");
                    return ;
                }
                var nsrxydjD = resData.nsrxydjD;
                if(nsrxydjD == "Y"){
                    alert("您的纳税信用等级是D级，办理发票业务请到前台办理。");
                    return ;
                }

                keyValue = resData.key;
                var reUrl = resData.reUrl;
                var param = "";
                if (keyValue != "") {
                    param = "cssParam=" + encodeURIComponent(keyValue) + "&xtBz=WSBS&";
                }
                if (reUrl != "") {
                    url = reUrl;
                }
                if (url.indexOf('?') == -1) {
                    if (tName == "税收法规") {
                        url = url + "?" + param + "r=" + parseInt(Math.random() * 10000, 10);
                    } else {
                        url = url + "?" + param + "r=" + Math.random();
                    }
                } else {
                    url = url + "&" + param + "r=" + Math.random();
                }
            }

            var h1 = screen.availHeight - 40;
            var w1 = screen.availWidth;
            var win = window.open(decodeURI(url)+"&isZero=1&isSmall=1");
            // win.focus();
            indexPages.push(win);
        }
    });
}

const sendSbList=(currentZeroInfo)=>{
  let timer = setInterval(() => {
    let indexContent = $('#indexContent');
    if(indexContent.length && indexContent.contents().find('#bizContent').length) {
      let bizContent = indexContent.contents().find('#bizContent');
      clearInterval(timer);
      timer = null;
      let timer1 = setInterval(() => {
        let number = 0;
        if(bizContent.contents().find('.datagrid-body table').length) {
          clearInterval(timer1);timer1 = null;
          let trs = bizContent.contents().find('.datagrid-body table tr');
          let sbAllList = Array.from(trs).map(item => {
            let tds = Array.from($(item).find("td")).filter(td => {
              return $(td).css('display') !== 'none';
            });
            let month = currentZeroInfo.endPeriod >= 10 ? currentZeroInfo.endPeriod : `0${currentZeroInfo.endPeriod}`;
            return {
              declareName: $(tds[1]).text().trim(),
              declareCycle: $(tds[2]).text() === '年报' ? 'month' : ($(tds[2]).text() === '季报' ? 'season' : 'month'),
              accountBook:currentZeroInfo.year + '-' + month,
              isSuccess: $(tds[7]).text().includes('已申报'),
              // isSuccess: false,
            }
          });
          let backInfo = {
            ...currentZeroInfo,
            recordList: sbAllList.concat(currentZeroInfo.recordList),
          };
          // 零申报结束
          window.postMessage(
            {
              event: 'zeroReportBack',
              data: backInfo,
            },
            '*'
          );
        }
        if (number > 20) {
          window.postMessage(
            {
              event: 'asyncTableDetailRequest',
              data: 'zeroTax'
            },
            '*'
          );
        }
        number ++;
      }, 500);
      return true;
    }
  }, 200);
};

const logout=()=>{
  window.sessionStorage.clear();
  $.ajax({
    type: 'post',
    data: 'postData={"lb":"out","ctrl":"WBLoginCtrl_logout"}',
    url: 'ajax.sword?ctrl=WBLoginCtrl_logout',
    success: function (resData) {
      var flag = resData.data[0].value;
      if (flag == "0") {
        var path = resData.data[1].value;
        var indexPages = window.indexPages;
        for (var i in indexPages) {
          if (indexPages[i] != window && indexPages[i] != null) indexPages[i].close();
        }
        window.location.replace(path);
      } else {
        alert("退出失败！");
      }
    },
    error: function (e) {
      alert("退出失败！");
    }
  });
}

export {
  openFakeWin,
  sendSbList,
  logout
};
